/**
 * Copyright(c) 2006-2008, FeyaSoft Inc.
 */
package("feyaSoft.source");

/**
 * This JS is mainly used to list source code with tree format.
 *
 * @author fzhuang
 * @Date Oct 6, 2007
 */
feyaSoft.source.ListSource = function(app, src) {
    // pre-define static variable
    //var ROOT_DIR = 'F:\\dayCareConn\\workspace\\FeyasoftWeb2\\';
    //var SEP = '\\';
     
    var ROOT_DIR = '/opt/application/feyasoft/';
    var SEP = '/';

    // pre-define parameters
    var desktop = app.getDesktop();
    var win = desktop.getWindow('listSource'); 
    
    Ext.QuickTips.init();

    // create javascript tree menu now
    var scriptTree = new Ext.tree.TreePanel({
        id:'script-tree',
        title: 'Client side - Javascripts',
        loader: new Ext.tree.TreeLoader(
	        {dataUrl:'source/listSource.htm?action=loadFiles'}
	    ),
		enableDrag:false,
        rootVisible:true,
        lines:false,
        autoScroll:true,
        iconCls:'script',
        
        root: new Ext.tree.AsyncTreeNode({
            text:'Javascript Sources',
            id: ROOT_DIR + 'WebContent' + SEP + 'public' + SEP + 'scripts'
        })        
    }); 
    
	// handle db click event and contextMenu
	scriptTree.on("dblclick", onClickHandler, this);	
	scriptTree.on('contextMenu', onContextMenu, this);
    
    // create src tree menu now
    var srcTree = new Ext.tree.TreePanel({
        id:'src-tree',
        title: 'Server side - J2EE code',
        tooltip:'Add a new user',
        loader: new Ext.tree.TreeLoader(
	        {dataUrl:'source/listSource.htm?action=loadFiles'}
	    ),
		enableDrag:false,
        rootVisible:true,
        lines:false,
        autoScroll:true,
        iconCls:'server',
        
        root: new Ext.tree.AsyncTreeNode({
            text:'Java Sources',
            id: ROOT_DIR + 'src' + SEP + 'com' + SEP + 'feyaSoft' + SEP + 'home'
        })       
    }); 
    
	// handle db click event and context menu
	srcTree.on("dblclick", onClickHandler, this);
	srcTree.on('contextMenu', onContextMenu, this);
	
	// script desc
	var scriptDesc = {
        title: 'JavaScript Code Structure',
        autoLoad: {url: '/public/html/javaScriptDesc.html', scripts: true},
        autoScroll:true,
        iconCls:'tutorials'    
    }; 
	
   /************************************************************
	* user double click the file now and show it in body part.
    ************************************************************/    
    function onClickHandler(node, obj, options) {
        if (!node.hasChildNodes()) {  
            // load stuff in center part...
            var comp = Ext.getCmp(node.text);
            
            if (!comp){
                // get lang type for file now
                var extn = node.text.split('.', 2);
				var lang;
				switch(extn[1]){
					case 'js':
						lang = 'javascript';
						break;
					case 'txt':
						lang = 'text';
						break;
					case 'css':
						lang = 'css';
						break;
					case 'html', 'htm':
						lang = 'html';
						break;
					case 'java':
						lang = 'java';
						break;
					default:
						lang = 'generic';
						break;
				}
				
				// get file name now		
                var nodeId = SEP + node.id.split(ROOT_DIR, 2)[1];  
                   
                centerPanel.add(new Ext.TemplateLayoutPanel({
                    title:node.text, 
                    closable:true, 
                    autoScroll: false, 
                    urlCode:nodeId, 
                    langCode:lang, 
                    treeNode: node})
                );

                comp = Ext.getCmp(node.text);
				centerPanel.setActiveTab(comp);
				
				comp.on('bodyresize', function(){
				    comp.doLayout();
				    comp.code.resize();
				}, this);
				
            } else{
				centerPanel.setActiveTab(comp);
			}
        } 
    }
    
   /************************************************************
	* handle context menu
    ************************************************************/ 
	function onContextMenu(node, e) {
	    if (!this.meun) {
	        this.menu = new Ext.menu.Menu({
		        id: 'sourceCode',
		        items: [{
		            id: 'download',
		            iconCls: 'download',
		            text: 'Download',
		            scope: this,
		            handler:function(){
		                window.open('source/listSource.htm?action=download&nodeId='+node.id+'&nodeName='+node.text);     
		            }
		        }]
		    });
		}
        node.select();
        this.menu.items.get('download')[node.attributes.leaf ? 'enable' : 'disable']();
        this.menu.showAt(e.getXY());
	}
    
   /************************************************************
	* define west panel
    ************************************************************/
    var westPanel = {
        region:'west',
        id:'west-panel',
        title:'Items',
        split:true,
        width:200,
        height:460,
        collapsible:true,
        margins:'3 0 3 3',
        cmargins:'3 3 3 3',
        layout:'accordion',
        layoutConfig: {animate:true},
        items: [
            scriptTree,
            srcTree
        ]
    };
    
   /************************************************************
	* define center tab panel
    ************************************************************/
    var centerPanel = new Ext.TabPanel({
        region: 'center',
        margins:'3 3 3 0', 
        activeTab: 0,
        deferredRender:false,
        enableTabScroll:true,
        monitorResize: true,

        items:[{
            title: 'Instruction',
            closable:false,
            autoScroll:true,
            autoLoad: {url: '/public/html/design/javaScriptDesc.html', scripts: true}
        }]
    });

   /************************************************************
	* define window now
    ************************************************************/
    if (!win) {
        win = desktop.createWindow({
            id: 'listSource',
            title:'FeyaSoft Source Code',
            width:900,
            height:480,
            x:20,
            y:20,
            iconCls: 'sourceCode',
            layoutConfig: {animate:false},
            shim:false,
            animCollapse:false,
            constrainHeader:true,
            layout: 'border',            
            
            items:[westPanel,centerPanel]
        });	            
    }
    win.show();    	        
};

/************************************************************
 * define code syntax for language
 ************************************************************/
Ext.TemplateLayoutPanel = function(config){

	this.urlCode = config.urlCode;
	this.title = config.title;
	this.langCode = config.langCode;
	this.treeNode = config.treeNode;
	if (this.urlCode){
		this.newDocument = false;
		this.code = new Ext.ux.CodePress({
			region: 'center',
			deferredRender: false,
			titlebar:false,
			autoScroll:false,
			monitorResize: true,
			id: 'codepressSource'
			, language: config.langCode
			, autoResize: true
			, url: this.urlCode
		});
	}
	else{
		this.newDocument = true;
		this.code = new Ext.ux.CodePress({
			region: 'center',
			deferredRender: false,
			titlebar:false,
			autoScroll:false,
			frame: true,
  		    monitorResize: true,
			id: 'codepressSource'
			, language: config.langCode
			, autoResize: true
			, code: ''
		});
	}
	Ext.TemplateLayoutPanel.superclass.constructor.call(this, {
		id: config.title,
		deferredRender: false,
		closable: true,
		layout:'border',
		monitorResize: true,
		tbar: this.tb,
		items: [
			this.code			
		]
	});	

};
Ext.extend(Ext.TemplateLayoutPanel, Ext.Panel, {
	onResize: function(w,h){
		Ext.TemplateLayoutPanel.superclass.onResize.call(this, w, h);
		this.code.resize();
	},

	onRender : function(callType, position){
        Ext.TemplateLayoutPanel.superclass.onRender.call(this, callType, position);
        // fix IE problem - not include 'px'
        Ext.BoxComponent.prototype.setSize('700','460');
	},
	
	afterRender : function() {
	    Ext.TemplateLayoutPanel.superclass.afterRender.call(this);
	    this.code.resize();
	},
	
	isDirty : function(){
		return this.code.isDirty();
	}
});

